import React, { Component } from 'react';
import { Tabs, Tree, Button, Modal, notification } from 'antd';
import axios from '../axios.config';


class Permitconfig extends Component {
    constructor(props) {
        super(props);
        this.state = {
            roles: [],
            permits: [],
            selected: undefined
        };
    }
    componentDidMount() {
        axios.get("Http/Msg/PermitconfigServer.ashx?method=Backend_BindRoleList").then(res => {
            if (res.data.flag === "True") {
                this.setState({ roles: res.data.result });
            }
        });
        axios.get("Http/Msg/PermitconfigServer.ashx?method=Backend_BindPermitList").then(res => {
            if (res.data.flag === "True") {
                this.setState({ permits: this._buildNode(null, res.data.result) });
                if (res.data.result.length > 0) { this.switchHanler(res.data.result[0].ZK_ID); }
            }
        });
    }
    _buildNode(parent, store) {
        var that = this;
        var obj = new Array();
        store.forEach(item => {
            if (!item.ZK_PARENT) {
                item.ZK_PARENT = null;
            }
            if (item.ZK_PARENT == parent) {
                obj.push({
                    id: item.ZK_ID,
                    label: item.ZK_NAME,
                    children: that._buildNode(item.ZK_ID, store)
                });
            }
        });
        return obj;
    }
    switchHanler(key) {
        axios.post("Http/Msg/PermitconfigServer.ashx?method=Backend_GetPermitList", { ROLE: key }).then(res => {
            if (res.data.flag === "True") {
                let selected = [];
                res.data.result.forEach(item => {
                    selected.push(item.ZK_PERMITID);
                });
                this.setState({ selected: selected });
            }
        });
    }
    updateconfig(role) {
        Modal.confirm({
            title: "温馨提示",
            content: "修改后将立即生效（需要把父级菜单同步选中），是否继续操作？",
            okText: "继续",
            cancelText: "取消",
            onOk: () => {
                axios.post("Http/Msg/PermitconfigServer.ashx?method=Backend_UpdatePermitConfig", {
                    ROLE: role, PERMITS: this.state.selected.join(",")
                }).then(res => {
                    console.log(res);
                    if (res.data.flag === "True") {
                        notification.success({
                            message: res.data.message,
                            description: "权限设置成功，对应角色的全部用户权限将同步变化"
                        });
                    }
                });
            }
        });
    }
    render() {
        let createTree = () => {
            if (this.state.permits.length > 0) {
                return <Tree
                    checkable
                    checkStrictly
                    defaultExpandAll
                    checkedKeys={this.state.selected}
                    onCheck={keys => { this.setState({ selected: keys.checked }); }}
                >
                    {this.state.permits.map(item => (buildTree(item)))}
                </Tree>
            } else {
                return "loading..."
            }
        }
        let buildTree = (item) => {
            if (item.children.length === 0) {
                return <Tree.TreeNode title={item.label} key={item.id} />
            } else {
                return <Tree.TreeNode title={item.label} key={item.id}>
                    {item.children.map(child => (buildTree(child)))}
                </Tree.TreeNode>
            }
        };
        return (
            <div id="permitconfig_temp">
                <Tabs onChange={key => { this.switchHanler(key); }}>
                    {this.state.roles.map(item => (
                        <Tabs.TabPane tab={item.ZK_ROLE} key={item.ZK_ID} >
                            {createTree()}
                            {this.state.permits.length > 0 ? <Button type="primary" onClick={() => { this.updateconfig(item.ZK_ID); }}>提交配置</Button> : ""}
                        </Tabs.TabPane>
                    ))}
                </Tabs>
            </div>
        );
    }
}

export default Permitconfig;
